package com.tools.common.container.r;

import com.tools.common.container.RowKit;
import com.tools.common.exception.InvalidOperationException;
import com.tools.common.object.Note;

import java.util.ArrayList;
import java.util.Collection;
import java.util.function.Predicate;
import java.util.function.UnaryOperator;

/**
 * 只读 ArrayList 集合
 * */
@Note("只读 ArrayList 集合")
public final class OnlyReadArrayList<T> extends ArrayList<T> {


    public OnlyReadArrayList() {
        super(0);
    }

    @SafeVarargs
    public OnlyReadArrayList(T... elements) {
        super(elements == null ? 0 : elements.length);
        if(elements == null) return;
        for(T obj : elements) super.add(obj);
    }

    public OnlyReadArrayList(Iterable<? extends T> elements) {
        super(RowKit.getIterableSize(elements));
        if(elements == null) return;
        for (T obj : elements) super.add(obj);
    }


    @Override
    public T set(int index, T element) {
        throw new InvalidOperationException("该 List 集合只支持读操作");
    }

    @Override
    public boolean add(T t) {
        throw new InvalidOperationException("该 List 集合只支持读操作");
    }

    @Override
    public void add(int index, T element) {
        throw new InvalidOperationException("该 List 集合只支持读操作");
    }

    @Override
    public T remove(int index) {
        throw new InvalidOperationException("该 List 集合只支持读操作");
    }

    @Override
    public boolean remove(Object o) {
        throw new InvalidOperationException("该 List 集合只支持读操作");
    }

    @Override
    public void clear() {
        throw new InvalidOperationException("该 List 集合只支持读操作");
    }

    @Override
    public boolean addAll(Collection<? extends T> c) {
        throw new InvalidOperationException("该 List 集合只支持读操作");
    }

    @Override
    public boolean addAll(int index, Collection<? extends T> c) {
        throw new InvalidOperationException("该 List 集合只支持读操作");
    }

    @Override
    protected void removeRange(int fromIndex, int toIndex) {
        throw new InvalidOperationException("该 List 集合只支持读操作");
    }

    @Override
    public boolean removeAll(Collection<?> c) {
        throw new InvalidOperationException("该 List 集合只支持读操作");
    }

    @Override
    public boolean retainAll(Collection<?> c) {
        throw new InvalidOperationException("该 List 集合只支持读操作");
    }

    @Override
    public boolean removeIf(Predicate<? super T> filter) {
        throw new InvalidOperationException("该 List 集合只支持读操作");
    }

    @Override
    public void replaceAll(UnaryOperator<T> operator) {
        throw new InvalidOperationException("该 List 集合只支持读操作");
    }
}
